// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { MaterialPalette, MaterialFontSettings } from "styling.slint";
import { LineEditBase } from "../common/lineedit-base.slint";

// Single line text input field with Material Design Outline TextField look and feel.
export component LineEdit {
    in property <length> font-size <=> base.font-size;
    in property <string> placeholder-text <=> base.placeholder-text;
    in property <bool> enabled <=> base.enabled;
    in property input-type <=> base.input-type;
    in property horizontal-alignment <=> base.horizontal-alignment;
    in property read-only <=> base.read-only;
    out property <bool> has-focus: base.has-focus;
    in-out property <string> text <=> base.text;

    callback accepted <=> base.accepted;
    callback edited <=> base.edited;
    callback key-pressed <=> base.key-pressed;
    callback key-released <=> base.key-released;
    accessible-role: text-input;
    accessible-enabled: root.enabled;
    accessible-value <=> text;
    accessible-placeholder-text: text == "" ? placeholder-text : "";
    accessible-read-only: root.read-only;
    accessible-action-set-value(v) => { text = v; edited(v); }

    public function set-selection-offsets(start: int, end: int) {
        base.set-selection-offsets(start, end);
    }

    public function select-all() {
        base.select-all();
    }
    public function clear-selection() {
        base.clear-selection();
    }
    public function cut() {
        base.cut();
    }
    public function copy() {
        base.copy();
    }
    public function paste() {
        base.paste();
    }

    min-width: max(120px, layout.min-width);
    min-height: max(56px, layout.min-height);
    forward-focus: base;

    states [
        disabled when !root.enabled : {
            background.border-color: MaterialPalette.control-foreground;
            background.opacity: 0.38;
            base.opacity: 0.38;
        }
        focused when root.has-focus : {
            background.border-width: 2px;
            background.border-color: MaterialPalette.accent-background;
        }
    ]

    background := Rectangle {
        width: 100%;
        height: 100%;
        border-radius: 4px;
        border-width: 1px;
        border-color: MaterialPalette.border;

        layout := HorizontalLayout {
            padding-left: 16px;
            padding-right: 16px;

            base := LineEditBase {
                text-color: MaterialPalette.foreground;
                font-size: MaterialFontSettings.body-large.font-size;
                font-weight: MaterialFontSettings.body-large.font-weight;
                selection-foreground-color: MaterialPalette.selection-foreground;
                margin: layout.padding-left + layout.padding-right;
                placeholder-color: MaterialPalette.border-variant;
                selection-background-color: MaterialPalette.selection-background;
            }
        }
    }
}
